/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * shoppingList.java
 *
 * Created on 2011-feb-28, 22:11:19
 */

package imat;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author Boel_
 */
public class shoppingList extends javax.swing.JPanel {

    /** Creates new form shoppingList */
    public shoppingList() {
        initComponents();

        shoppingListRowScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        shoppingListRowScrollPane.getVerticalScrollBar().setUnitIncrement(16);

        for(int i = 0; i < 5; i++) {
            shoppingListAddRowActionPerformed(null);
        }
        saveButton.setEnabled(false);

        shoppingListName.getDocument().addDocumentListener(
            new DocumentListener() {
                public void changedUpdate(DocumentEvent e) {
                    if(!shoppingListName.getText().equals("")) {
                        saveButton.setEnabled(true);
                    }
                }

                public void removeUpdate(DocumentEvent e) {
                    if(!shoppingListName.getText().equals("")) {
                        saveButton.setEnabled(true);
                    }
                }
                public void insertUpdate(DocumentEvent e) {
                    if(!shoppingListName.getText().equals("")) {
                        saveButton.setEnabled(true);
                    }
                }
            }
        );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        shoppingListTopPanel = new javax.swing.JPanel();
        shoppingListHeader = new javax.swing.JLabel();
        shoppingListName = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        shoppingListBottomPanel = new javax.swing.JPanel();
        shoppingListRowScrollPane = new javax.swing.JScrollPane();
        shoppingListRowPanel = new javax.swing.JPanel();
        shoppingListAddRow = new javax.swing.JButton();

        setName("Form"); // NOI18N

        shoppingListTopPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        shoppingListTopPanel.setName("shoppingListTopPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(imat.IMatApp.class).getContext().getResourceMap(shoppingList.class);
        shoppingListHeader.setFont(resourceMap.getFont("shoppingListHeader.font")); // NOI18N
        shoppingListHeader.setText(resourceMap.getString("shoppingListHeader.text")); // NOI18N
        shoppingListHeader.setName("shoppingListHeader"); // NOI18N

        shoppingListName.setFont(resourceMap.getFont("shoppingListName.font")); // NOI18N
        shoppingListName.setText(resourceMap.getString("shoppingListName.text")); // NOI18N
        shoppingListName.setToolTipText(resourceMap.getString("shoppingListName.toolTipText")); // NOI18N
        shoppingListName.setName("shoppingListName"); // NOI18N
        shoppingListName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                shoppingListNameFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                shoppingListNameFocusLost(evt);
            }
        });

        saveButton.setFont(resourceMap.getFont("saveButton.font")); // NOI18N
        saveButton.setIcon(resourceMap.getIcon("saveButton.icon")); // NOI18N
        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setToolTipText(resourceMap.getString("saveButton.toolTipText")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout shoppingListTopPanelLayout = new javax.swing.GroupLayout(shoppingListTopPanel);
        shoppingListTopPanel.setLayout(shoppingListTopPanelLayout);
        shoppingListTopPanelLayout.setHorizontalGroup(
            shoppingListTopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(shoppingListName, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, shoppingListTopPanelLayout.createSequentialGroup()
                .addComponent(shoppingListHeader, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveButton))
        );
        shoppingListTopPanelLayout.setVerticalGroup(
            shoppingListTopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(shoppingListTopPanelLayout.createSequentialGroup()
                .addGroup(shoppingListTopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(shoppingListHeader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(saveButton, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(shoppingListName, javax.swing.GroupLayout.PREFERRED_SIZE, 28, Short.MAX_VALUE))
        );

        shoppingListBottomPanel.setName("shoppingListBottomPanel"); // NOI18N

        javax.swing.GroupLayout shoppingListBottomPanelLayout = new javax.swing.GroupLayout(shoppingListBottomPanel);
        shoppingListBottomPanel.setLayout(shoppingListBottomPanelLayout);
        shoppingListBottomPanelLayout.setHorizontalGroup(
            shoppingListBottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );
        shoppingListBottomPanelLayout.setVerticalGroup(
            shoppingListBottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 14, Short.MAX_VALUE)
        );

        shoppingListRowScrollPane.setName("shoppingListRowScrollPane"); // NOI18N

        shoppingListRowPanel.setName("shoppingListRowPanel"); // NOI18N
        shoppingListRowPanel.setLayout(new javax.swing.BoxLayout(shoppingListRowPanel, javax.swing.BoxLayout.Y_AXIS));
        shoppingListRowScrollPane.setViewportView(shoppingListRowPanel);

        shoppingListAddRow.setFont(resourceMap.getFont("shoppingListAddRow.font")); // NOI18N
        shoppingListAddRow.setIcon(resourceMap.getIcon("shoppingListAddRow.icon")); // NOI18N
        shoppingListAddRow.setText(resourceMap.getString("shoppingListAddRow.text")); // NOI18N
        shoppingListAddRow.setToolTipText(resourceMap.getString("shoppingListAddRow.toolTipText")); // NOI18N
        shoppingListAddRow.setName("shoppingListAddRow"); // NOI18N
        shoppingListAddRow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                shoppingListAddRowActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(shoppingListTopPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(247, 247, 247)
                .addComponent(shoppingListBottomPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(shoppingListRowScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(shoppingListAddRow, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(37, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(shoppingListTopPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(shoppingListRowScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(shoppingListBottomPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(shoppingListAddRow)
                        .addContainerGap())))
        );
    }// </editor-fold>//GEN-END:initComponents
    public void shoppingListLoad(){
        shoppingListAddRowActionPerformed(null);
    }
    public void addRow(ShoppingListRow s){
        saveButton.setEnabled(true);
        ShoppingListRow q = new ShoppingListRow();
        q.setRowItemTextField(s.getRowItemTextField());
        shoppingListRowPanel.add(q);
        shoppingListRowPanel.revalidate();
        shoppingListRowPanel.repaint();
    }
    public void addName(String s){
        shoppingListName.setText(s);
    }
    private void shoppingListAddRowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shoppingListAddRowActionPerformed
        saveButton.setEnabled(true);
        shoppingListRowPanel.add(new ShoppingListRow());
        shoppingListRowPanel.revalidate();
        shoppingListRowPanel.repaint();
    }//GEN-LAST:event_shoppingListAddRowActionPerformed

    private void shoppingListNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_shoppingListNameFocusGained
        if(shoppingListName.getText().equals("Skriv listans namn här")) {
            shoppingListName.setText("");
        }
    }//GEN-LAST:event_shoppingListNameFocusGained

    private void shoppingListNameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_shoppingListNameFocusLost
        if(shoppingListName.getText().equals("")) {
            shoppingListName.setText("Skriv listans namn här");
            saveButton.setEnabled(false);
        }
    }//GEN-LAST:event_shoppingListNameFocusLost
    public void clearAll(){
        shoppingListRowPanel.removeAll();
    }
    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        boolean overwrite=false;
        DataInputStream dis;
        FileWriter      file;
        String          eol = System.getProperty("line.separator");

        if(!shoppingListName.getText().equals("")){
            try {
                dis = new DataInputStream(new BufferedInputStream(new FileInputStream("lists.txt")));
                while (dis.available() != 0) {
                    if(shoppingListName.getText().equals(dis.readLine())){
                       overwrite=true;
                    }
                }
                dis.close();
            }
            catch(Exception e) {
            }
        }

        try {
            file = new FileWriter(shoppingListName.getText()+".txt",false);
            ShoppingListRow temp;

            for(int i = 0; i < shoppingListRowPanel.getComponentCount(); i++) {
                temp = (ShoppingListRow) shoppingListRowPanel.getComponent(i);
                file.write(temp.getRowItemTextField() + eol);
            }
            file.close();
            if(!overwrite){
                file = new FileWriter("lists.txt",true);
                file.write(shoppingListName.getText() + eol);
                file.close();
            }
        }
        catch (IOException e){
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    public JButton getSaveButton() {
        return saveButton;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton saveButton;
    private javax.swing.JButton shoppingListAddRow;
    private javax.swing.JPanel shoppingListBottomPanel;
    private javax.swing.JLabel shoppingListHeader;
    private javax.swing.JTextField shoppingListName;
    private javax.swing.JPanel shoppingListRowPanel;
    private javax.swing.JScrollPane shoppingListRowScrollPane;
    private javax.swing.JPanel shoppingListTopPanel;
    // End of variables declaration//GEN-END:variables

}
